package com.tydic.mapboxtest.util;

import com.mapbox.mapboxsdk.geometry.LatLng;

import java.util.List;

/**
 * Created by hechao on 17/4/10.
 */

public class MapTool {

    /**
     * 判断坐标点是否落在指定的多边形区域内
     * @param point  指定的坐标点
     * @param list   多变形区域的节点集合
     * @return   True 落在范围内 False 不在范围内
     */
    public static boolean IsWithIn(LatLng point, List<LatLng> list) {
        double x = point.getLatitude();
        double y = point.getLongitude();

        int isum, icount, index;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;

        if (list.size() < 3) {
            return false;
        }

        isum = 0;
        icount = list.size();

        for (index = 0; index < icount - 1; index++) {
            if (index == icount - 1) {
                dLon1 = list.get(index).getLatitude();
                dLat1 = list.get(index).getLongitude();
                dLon2 = list.get(0).getLatitude();
                dLat2 = list.get(0).getLongitude();
            } else {
                dLon1 = list.get(index).getLatitude();
                dLat1 = list.get(index).getLongitude();
                dLon2 = list.get(index + 1).getLatitude();
                dLat2 = list.get(index + 1).getLongitude();
            }

            // 判断指定点的 纬度是否在 相邻两个点(不为同一点)的纬度之间
            if (((y >= dLat1) && (y < dLat2)) || ((y >= dLat2) && (y < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - y)) / (dLat1 - dLat2);
                    if (dLon < x){
                        isum++;
                    }
                }
            }
        }

        if ((isum % 2) != 0) {
            return true;
        } else {
            return false;
        }
    }
}
